using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

namespace AdobePhotoElementsMetatagger
{
    class ElementsTagHelper
    {
        private Dictionary<int,ElementsTag> _tags = new Dictionary<int, ElementsTag>();

        internal Dictionary<int,ElementsTag> Tags
        {
            get { return _tags; }
            set { _tags = value; }
        }

        public ElementsTagHelper(ElementsDS.FolderTableDataTable folderTable)
        {
            this.GenerateTags(folderTable);
            this.GeneratePaths();
        }

        private void GenerateTags(ElementsDS.FolderTableDataTable folderTable)
        {
            foreach (ElementsDS.FolderTableRow row in folderTable.Rows)
            {
                ElementsTag tag;
                //check for the item first. If it is already there then it will get updated
                if(!this.Tags.TryGetValue(row.fFolderId,out tag))
                {
                    tag = new ElementsTag();
                    tag.Number = row.fFolderId;
                    this.Tags.Add(tag.Number, tag);
                }
                
                tag.Name = row.fFolderName;

                //if I have a parent then add a node for them to so that I can add myself as a child
                if(row.fParentFolderId != 0)
                {
                    ElementsTag parent;
                    if(!this.Tags.TryGetValue(row.fParentFolderId,out parent))
                    {
                        parent = new ElementsTag();
                        parent.Number = row.fParentFolderId;
                        this.Tags.Add(parent.Number,parent);
                    }
                    parent.Children.Add(tag);
                    tag.Parent = parent;
                }
            }
        }

        private void GeneratePaths()
        {
            foreach (ElementsTag tag in this.Tags.Values)
            {
                tag.Path = GetTagPath(tag);
            }
        }

        public String GetTagPath(int tagNumber)
        {
            ElementsTag tag;
            if (!this.Tags.TryGetValue(tagNumber, out tag))
            {
                throw new ApplicationException(String.Format("Tag {0} Not Found", tagNumber));
            }
            return GetTagPath(tag);
        }

        public String GetTagPath(ElementsTag tag)
        {
            if(tag.Parent != null)
            {
                if (tag.Name != String.Empty)
                {
                    return GetTagPath(tag.Parent) + "/" + tag.Name;
                }
                else
                {
                    return GetTagPath(tag.Parent);
                }
            }
            return tag.Name;
        }

        private void SetTagValues(ref ElementsTag tag, ref ElementsDS.FolderTableRow row)
        {
            tag.Number = row.fFolderId;
            tag.Name = row.fFolderName;
        }

        internal void AddTagsToPicture(ElementsPicture pic, int[] tagIds)
        {
            for (int i = 0; i < tagIds.Length; i++)
            {
                if (tagIds[i] == 0) break;
                pic.Tags.Add(_tags[tagIds[i]]);
                pic.Keywords.Add(_tags[tagIds[i]].Path);
            }
        }
    }
}
